<?php 

defined('_JEXEC') or die;

abstract class modSEventLastEvents
{
	public static function getList(&$params)
	{
		$list_to_display = array();
		
		$nr = $params->get('count_of_events');
		
		if($nr)
		{
			$db = JFactory::getDbo();
			
			// Initialize the query object
			$query = $db->getQuery(true);
			
			$rows = array();
			
			$rows[] = 'type.event_type';
			
			if($params->get('show_author'))
			{
				$rows[] = 'user.name';
			}
			
			$rows[] = 'ev.date_time';
			
			if($params->get('show_comment'))
			{
				$rows[] = 'ev.comment';
			}
			
				
			// Query the database to get the author information for the current article
			$query->select($rows);
			$query->from('#__sevent_events AS ev');
			$query->leftJoin('#__sevent_event_type AS type ON ev.event_type = type.id');
			$query->leftJoin('#__users AS user ON ev.user_id = user.id');
			$query->order('ev.date_time DESC');
			
			if($params->get('show_only_active_events'))
			{
				$query->where('ev.date_time >= SYSDATE()');
			}
			
			$db->setQuery($query, 0 , $params->get('count_of_events'));
			$items = $db->loadObjectList();
			
			
			
			foreach ($items as $item)
			{
				if(($now < $item->date_time ) || (!$params->get('show_only_active_events')))
				{
					if(!$params->get('show_date_time'))
					{
						unset($item->date_time);		
					}
					else 
					{
						$item->date_time = substr_replace($item->date_time ,"",-3);
					}
					
					if($params->get('show_comment'))
					{
						$item->comment = JHtml::_('string.truncate', $item->comment, $params->get('char_number_of_comment'));
					}
				
					$list_to_display[] = $item;
				}
			}
		}
		
		return $list_to_display;
	}
}